//
//  HLInquireVC.m
//  zhuanxian
//
//  Created by zhaoliang chen on 2017/11/17.
//  Copyright © 2017年 huiling. All rights reserved.
//

#import "HLInquireVC.h"
#import "HLWaybillBottomView.h"
#import "HLWaybillCell.h"
#import "HLWaybillDetailVC.h"
#import "HLFiltrateView.h"
#import "CustomPopOverView.h"
#import "ActionSheetDatePicker.h"
#import "HLWaybillFiltrateObject.h"
#import "HLWaybillObject.h"
#import "HLWaybillDetailObject.h"

@interface HLInquireVC ()
<UITableViewDelegate,UITableViewDataSource,HLWaybillDetailVCDelegate>
{
    UIBarButtonItem* rightItem ;
    NSInteger pageNumber;
    NSInteger pageSize;
}

@property(nonatomic,strong)HLWaybillBottomView* bottomView;
@property(nonatomic,strong)UITableView* tableViewWaybill;
@property(nonatomic,strong)NSMutableArray* arrayWaybills;
@property(nonatomic,strong)HLWaybillFiltrateObject* filtrateObject;

@end

@implementation HLInquireVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setNavigationTitle:@"运单查询" Color:[UIColor whiteColor]];
    self.filtrateObject = [[HLWaybillFiltrateObject alloc]init];
    self.arrayWaybills = [NSMutableArray new];
    rightItem = [self addRightNavigationBarButtonImage:nil Title:@"筛选" Target:self Action:@selector(onClickFiltrate)];
    pageNumber = 1;
    pageSize = 10;
    
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.height.mas_equalTo(50);
    }];
    
    [self.view addSubview:self.tableViewWaybill];
    [self.tableViewWaybill mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.bottomView.mas_top);
    }];
    if (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0) {
        self.tableViewWaybill.estimatedSectionHeaderHeight = 0.f;
        self.tableViewWaybill.estimatedSectionFooterHeight = 0.f;
    }
    
    [self.tableViewWaybill.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)onClickFiltrate {
    WeakSelf;
    HLFiltrateView* view = [[HLFiltrateView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_MAX_WIDTH-30, 410)];
    PopOverVieConfiguration *config = [PopOverVieConfiguration new];
    config.showSpace = 0;
    config.roundMargin = 0;
    config.triAngelWidth = 15;
    config.triAngelHeight = 7;
    CustomPopOverView* popupView = [CustomPopOverView popOverView];
    popupView.config = config;
    popupView.containerBackgroudColor = view.backgroundColor;
    popupView.clipsToBounds = YES;
    popupView.content = view;
    [popupView showFrom:rightItem.customView alignStyle:CPAlignStyleRight];
    view.onCancelFilter = ^{
        [popupView dismiss];
    };
    view.onConfirmFilter = ^(HLWaybillFiltrateObject *obj) {
        [popupView dismiss];
        weakSelf.filtrateObject = obj;
        [weakSelf getWaylist:YES];
    };
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayWaybills.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HLWaybillCell* cell = [tableView dequeueReusableCellWithIdentifier:@"HLWaybillCell"];
    if (!cell) {
        cell = [[HLWaybillCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HLWaybillCell"];
    }
    cell.waybillObject = self.arrayWaybills[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HLWaybillObject* waybillObject = self.arrayWaybills[indexPath.row];
    HLWaybillDetailVC* vc = [[HLWaybillDetailVC alloc]initWithWaybillDetail:waybillObject.waybill_id];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return 200;
//}

- (void)onScrapWaybillCompletion:(HLWaybillDetailObject*)waybill {
    for (HLWaybillObject* waybillObject in self.arrayWaybills) {
        if ([waybillObject.waybill_id isEqualToString:waybill.waybillId]) {
            [self.arrayWaybills removeObject:waybillObject];
            break;
        }
    }
    [self.tableViewWaybill reloadData];
}

- (void)getWaylist:(BOOL)refresh {
    if (refresh) {
        pageNumber = 1;
    } else {
        pageNumber++;
    }
    [HLWebManager getWaybillList:self.filtrateObject pageNumber:pageNumber pageSize:pageSize successBlock:^(id response) {
        if (refresh) {
            [self.tableViewWaybill.mj_header endRefreshing];
        }
        if ([response[@"code"]integerValue]==1) {
            if (refresh) {
                [self.arrayWaybills removeAllObjects];
                self.bottomView.labelTotalCount.text = [NSString stringWithFormat:@"%@",response[@"billCount"]];
                self.bottomView.labelFreight.text = [NSString stringWithFormat:@"%@",response[@"totalFees"]];
                self.bottomView.labelCollection.text = [NSString stringWithFormat:@"%@",response[@"totalCollectionProcurationFees"]];
            }
            NSArray* array = [HLWaybillObject mj_objectArrayWithKeyValuesArray:response[@"list"]];
            if (array.count > 0) {
                if (!refresh) {
                    [self.tableViewWaybill.mj_footer endRefreshing];
                }
                [self.arrayWaybills addObjectsFromArray:[HLWaybillObject mj_objectArrayWithKeyValuesArray:response[@"list"]]];
            } else {
                if (!refresh) {
                    [self.tableViewWaybill.mj_footer endRefreshingWithNoMoreData];
                    pageNumber--;
                }
            }
            [self.tableViewWaybill reloadData];
        } else {
            [SVProgressHUD showErrorWithStatus:response[@"message"]];
        }
    } failureBlock:^(NSError *error) {
        [self.tableViewWaybill.mj_header endRefreshing];
    }];
}

- (HLWaybillBottomView*)bottomView {
    if (!_bottomView) {
        _bottomView = [[HLWaybillBottomView alloc]initWithFrame:CGRectZero];
    }
    return _bottomView;
}

- (UITableView*)tableViewWaybill {
    if (!_tableViewWaybill) {
        _tableViewWaybill = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableViewWaybill.dataSource = self;
        _tableViewWaybill.delegate = self;
        WeakSelf;
        _tableViewWaybill.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf getWaylist:YES];
        }];
        _tableViewWaybill.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [weakSelf getWaylist:NO];
        }];
        _tableViewWaybill.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableViewWaybill.backgroundColor = BackColor;
        _tableViewWaybill.rowHeight = UITableViewAutomaticDimension;
        _tableViewWaybill.estimatedRowHeight = 100;
    }
    return _tableViewWaybill;
}

@end
